package santiago.domain;

import java.io.Serializable;
import java.util.Stack;
import santiago.exception.InvalidOperationException;

public class Plantation implements Serializable {

    private PlantationType Type;
    private short Capacity;
    private boolean Desert;
    private Stack<YieldMarker> YieldMarkers;

    private Plantation() {
        Capacity = 1;
        Desert = false;
        YieldMarkers = new Stack<>();
    }

    public Plantation(PlantationType Type) {
        this();
        this.Type = Type;
    }

    public Plantation(PlantationType Type, short Capacity) {
        this(Type);
        this.Capacity = Capacity;
    }

    public void addYieldMarker(YieldMarker y) {
        this.YieldMarkers.push(y);
    }

    /**
     * Dry up a plantation until it turn into desert
     */
    public void dryUp() throws InvalidOperationException {
        if (!Desert) {
            if (YieldMarkers.isEmpty()) {
                throw new InvalidOperationException();
            }
            YieldMarkers.pop();
        }
    }

    /**
     * Turn a field into desert
     */
    public void turnIntoDesert() {
        if (!Desert) {
            YieldMarkers.clear();
            Desert = true;
        }
    }

    /**
     * @return Score of the field, counting 1 point by YieldMarker and Palmtree
     */
    public byte getScore() {
        return (byte) YieldMarkers.size();
    }

    public boolean isDesert() {
        return Desert;
    }

    public PlantationType getType() {
        return Type;
    }

    public short getCapacity() {
        return Capacity;
    }

    public short getYieldMarkersCount() {
        return (short) YieldMarkers.size();
    }

    public boolean isPlayerPlantation(Player Player) {
        if (YieldMarkers.isEmpty()) {
            return false;
        }
        return Player.getColor().equals(YieldMarkers.get(0).getColor());
    }

    public ColorType getColor() {
        if (!YieldMarkers.isEmpty()) {
            return YieldMarkers.get(0).getColor();
        }
        return null;
    }

    @Override
    public String toString() {
        return Type.toString() + " (" + Capacity + ")";
    }
}
